package com.bolong.gmgc.timer.monthcard;

import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bolong.gmgc.bean.record.CarMonthQuery;
import com.bolong.gmgc.bean.record.CarRecord;
import com.bolong.gmgc.bean.record.MonthCard;
import com.bolong.gmgc.bean.record.MonthCardQuery;
import com.bolong.gmgc.bean.record.MonthCardQuery.Criteria;
import com.bolong.gmgc.dao.record.CarMonthMapper;
import com.bolong.gmgc.dao.record.CarRecordMapper;
import com.bolong.gmgc.dao.record.MonthCardMapper;

/**
 * <th>月卡过期处理具体实现类</th>
 * 
 * @author decepeng
 *
 */
@Component
public class MonthCardExpiredTask extends TimerTask {

	@Autowired
	private MonthCardMapper monthCardMapper; // 月卡

	@Autowired
	private CarRecordMapper carRecordMapper; // 停车记录

	@Autowired
	private CarMonthMapper carMonthMapper; // 月卡关联表

	@Override
	public void run() {

		Date date = new Date();
		System.out.println("<---------------------批量月卡过期操作开始--------------------->");
		MonthCardQuery monthCardQuery = new MonthCardQuery();
		monthCardQuery.setOrderByClause("park_id asc");
		Criteria createCriteria = monthCardQuery.createCriteria();
		createCriteria.andCardStatusEqualTo(1); // 正常月卡
		createCriteria.andCardZxstatusEqualTo(1); // 未注销月卡
		createCriteria.andCardTermofvalidityLessThan(date); // 当前时间
		System.out.println("业务条件时间为---->" + date);

		// 月卡集合
		List<MonthCard> monthCardList = monthCardMapper.selectByExample(monthCardQuery);

		System.out.println("过期月卡条数为---->" + monthCardList.size());

		for (MonthCard monthCard : monthCardList) {

			// 修改月卡对象
			MonthCard monthCardUpdate = new MonthCard();

			// 查询月卡关联表
			CarMonthQuery carMonthQuery = new CarMonthQuery();
			carMonthQuery.createCriteria().andMonthcardIdEqualTo(monthCard.getCardId());
			long carMonthCount = carMonthMapper.countByExample(carMonthQuery);

			// 根据进场时间进行排序
			String orderBy = carMonthCount > 1 ? "record_intime ASC" : "record_intime DESC";

			// 过期在线车辆集合
			List<CarRecord> carRecordList = carRecordMapper.expiredMonthcardRecord(monthCard.getCardId(), monthCard.getParkId(), orderBy);

			System.out.println("查询到记录数为---->" + carRecordList.size());
			for (int i = 0; i < carRecordList.size(); i++) {

				CarRecord record = new CarRecord();

				if (i == 0) {
					System.out.println("----->正在停车记录为>" + carRecordList.get(i).getId() + ">车牌号为--->" + carRecordList.get(i).getCarNumber());
					record.setId(carRecordList.get(i).getId());
					record.setRecordIntime(monthCard.getCardTermofvalidity());
				} else if (i > carMonthCount) {
					record.setId(carRecordList.get(i).getId()); // id
					record.setRecordTimelength(0);// 停车时长
					record.setRecordPaymoney(0.00);// 金额
					record.setRedordPaytype(0);// 类型免费
					record.setRecordType(1);// 已缴费
					record.setRecordStatus(0);// 历史车辆
					record.setRemarks("月卡过期，批量处理事件");// 备注
				}

				int updateByPrimaryKeySelective = carRecordMapper.updateByPrimaryKeySelective(record);
				System.out.println(">修改记录数为--->" + updateByPrimaryKeySelective);

			}
			monthCardUpdate.setCardId(monthCard.getCardId());// 月卡id
			monthCardUpdate.setCardStatus(5);// 修改月卡状态为过期
			int updateMonthCard = monthCardMapper.updateByPrimaryKeySelective(monthCardUpdate);

			System.out.println("------月卡过期时间为>" + monthCard.getCardTermofvalidity() + ">修改月卡过期数量--->" + updateMonthCard);

		}

		System.out.println("<---------------------批量月卡过期操作结束--------------------->");
	}

}
